package com.neouton.crm.dictionary.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neouton.common.util.AuthUtils;
import com.neouton.crm.dictionary.entity.DictionaryLookupItemVO;
import com.neouton.crm.dictionary.entity.DictionaryLookupVO;
import com.neouton.crm.dictionary.entity.SysMenuVO;
import com.neouton.crm.dictionary.mapper.DictionaryMapper;
import com.neouton.crm.dictionary.service.IDictionaryService;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.feginClient.FeginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author liye
 * @Date 2022/6/27 14:50
 * @Version 1.0
 */
@Service
public class DictionaryServiceImpl implements IDictionaryService {

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private FeginService feginService;

    @Override
    public IPage<DictionaryLookupVO> queryDictionarysByPage(IPage page, DictionaryLookupVO dataDictionaryLookup) {
        IPage<DictionaryLookupVO> dataDictionaryList = dictionaryMapper.queryDictionarysByPage(page, dataDictionaryLookup);
        return dataDictionaryList;
    }

    @Override
    public Map<String, List<DictionaryLookupItemVO>> queryDictionaryLookupInfo(Integer status, List<String> classfiedCode){
        if ("null".equals(classfiedCode)) {
            classfiedCode = null;
        }
        List<DictionaryLookupItemVO> dataDictionaryLookupItems = dictionaryMapper.queryLookupItemInfo(status, classfiedCode);
        Map<String, List<DictionaryLookupItemVO>>  dictionaryLookupInfo = dataDictionaryLookupItems.stream().collect(Collectors.groupingBy(DictionaryLookupItemVO::getClassfiedCode));
        return dictionaryLookupInfo;
    }

    @Override
    public Boolean addDictionaryLookupItem(Integer id, List<DictionaryLookupItemVO> dictionaryLookupItems) {
        List<DictionaryLookupItemVO> items = dictionaryLookupItems.stream().filter(item -> !StringUtils.isBlank(item.getItemCode())).collect(Collectors.toList());
        return dictionaryMapper.insertDictionaryLookupItem(id, items);
    }

    /**
     * 获取各模块管理员
     * @param page
     * @param sysMenuVO
     * @return
     */
    public IPage<SysMenuVO> queryCrmAdminByPage(IPage page, SysMenuVO sysMenuVO){
        IPage<SysMenuVO> sysMenuVOIPage = dictionaryMapper.queryCrmAdminByPage(page, sysMenuVO);
        return sysMenuVOIPage;
    }

    /**
     * 获取管理员集合
     * @param menuCode
     * @return
     */
    public List<SysUserEntity> queryMenuUserIdsByCode(String menuCode){
        return dictionaryMapper.queryMenuUserIdsByCode(menuCode);
    }

    /**
     * 保存管理员信息
     * @param menuId
     * @return
     */
    public Boolean saveMenuUserIds(Integer menuId, List<SysUserEntity> users){
        return dictionaryMapper.saveMenuUserIds(menuId,users,AuthUtils.getUserId());
    }

    /**
     * 查询是否为管理员
     * @return
     */
    public Boolean queryIsMenuUserByUserId(){
        return dictionaryMapper.queryIsMenuUserByUserId();
    }


    /**
     * 获取抄送人列表
     * @param page
     * @param sysMenuVO
     * @return
     */
    public IPage<SysMenuVO> queryCrmCcByPage(IPage page, SysMenuVO sysMenuVO,String token){
        //获取全部员工
        List<SysUserEntity> invalidUsers = feginService.queryAllInvalidUsers(token).getResult();
        //删除全部失效员工
        dictionaryMapper.delectCrmCcUserByInvalidUsers(invalidUsers);

        IPage<SysMenuVO> sysMenuVOIPage = dictionaryMapper.queryCrmCcByPage(page, sysMenuVO);
        return sysMenuVOIPage;
    }

    /**
     * 获取抄送人
     * @param eventCode
     * @return
     */
    public List<SysUserEntity> queryCcUserIdsByCode(String eventCode){
        return dictionaryMapper.queryCcUserIdsByCode(eventCode);
    }

    /**
     * 保存抄送人信息
     * @param eventId
     * @return
     */
    public Boolean saveCcUserIds(Integer eventId,List<SysUserEntity> users){
        return dictionaryMapper.saveCcUserIds(eventId,users, AuthUtils.getUserId());
    }

    @Override
    public IPage<SysMenuVO> queryCrmAuthByPage(IPage page, SysMenuVO sysMenuVO) {
        IPage<SysMenuVO> sysMenuVOIPage = dictionaryMapper.queryCrmAuthByPage(page, sysMenuVO);
        return sysMenuVOIPage;
    }

    @Override
    public List<SysUserEntity> queryAuthUserIdsByCode(String authCode) {
        return dictionaryMapper.queryAuthUserIdsByCode(authCode);
    }

    @Override
    public Boolean saveAuthUserIds(Integer authManageId, List<SysUserEntity> users) {
        return dictionaryMapper.saveAuthUserIds(authManageId,users, AuthUtils.getUserId());
    }
}




